package com.tjsinfo.tjsoa.documentationflow.dangweifawen.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.dao.ITDangweifawenNeirongDAO;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenBeizhu;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenFujian;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenNeirong;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenZhengwen;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenFujian;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TaoHongMoBan;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.YinZhang;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenNeirong;
import com.tjsinfo.tjsoa.message.vo.TjsMessage;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemPost;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class TDangweifaweiNeirongBO implements ITDangweifawenNeirongBO	{
	private ITDangweifawenNeirongDAO dangweifawenNeirongDAO;
	private IQbcDao qbcDao;
	private ISystemUserBo systemUserBo;
	private ISpringDao springDao;
	
	
	
	public ISpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}

	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	

	public ITDangweifawenNeirongDAO getDangweifawenNeirongDAO() {
		return dangweifawenNeirongDAO;
	}

	public void setDangweifawenNeirongDAO(
			ITDangweifawenNeirongDAO dangweifawenNeirongDAO) {
		this.dangweifawenNeirongDAO = dangweifawenNeirongDAO;
	}

	public String addTDangweifaweineirong(
			TDangweifawenNeirong dangweifawenNeirong,String hegaorenid,String huiqian,String zhusongid,String chaosongid,List<Files> list) {
		// TODO Auto-generated method stub
		
		String id = dangweifawenNeirongDAO.save(dangweifawenNeirong);
		
		List<String> fenfalist = new ArrayList<String>();
		List<String> huiqianlist = new ArrayList<String>();
		String[] zhusongarray = zhusongid.split(",");
		for (int i = 0; i < zhusongarray.length; i++) {
			if (zhusongarray[i] !=null &&!"".equals(zhusongarray[i])) {
				fenfalist.add(zhusongarray[i]);
			}
		}
		if (chaosongid != null) {
			String[] chaosongarray = chaosongid.split(",");	
			for (int i = 0; i < chaosongarray.length; i++) {
				if (chaosongarray[i] !=null &&!"".equals(chaosongarray[i])) {
					fenfalist.add(chaosongarray[i]);
				}
			}
		}
		String huiqianid="1";
		if (huiqian != null) {     
			String[] huiqianarray = huiqian.split(",");	
			if (huiqianarray[0] !=null &&!"".equals(huiqianarray[0])) {
				huiqianid=huiqianarray[0];
			}
			for (int i = 1; i < huiqianarray.length; i++) {
				if (huiqianarray[i] !=null &&!"".equals(huiqianarray[i])) {
					huiqianlist.add(huiqianarray[i]);
				}
			}
		}
		WorkFlowTemplate flowTemplate = new WorkFlowTemplate();
		Long flowid = 0l;
		if (hegaorenid !=null && !"".equals(hegaorenid)) {
				flowid =  flowTemplate.creatProcessInstance("部门审核", hegaorenid, "党委发文");
				flowTemplate.setVariable("流程名称", "党委发文", flowid);
			
			fenfalist = systemUserBo.userlistOrdery(fenfalist);
			flowTemplate.setVariable("封发", fenfalist, flowid);
		} else  if (huiqian !=null && !"".equals(huiqian)){
			flowid =  flowTemplate.creatProcessInstance("部门主管会签", huiqianid, "党委发文","to 部门主管会签");
			flowTemplate.setVariable("流程名称", "党委发文", flowid);
			flowTemplate.setVariable("部门会签顺序组", huiqianlist, flowid);
		
			fenfalist = systemUserBo.userlistOrdery(fenfalist);
			flowTemplate.setVariable("封发", fenfalist, flowid);
		}else{
			List<TSystemUser>  systemuserlist = new ArrayList<TSystemUser>();
			
			systemuserlist = systemUserBo.selectByDutyName("办公室主任");
				
			TSystemUser systemUser = systemuserlist.get(0);
				flowid =  flowTemplate.creatProcessInstance("办公室主任复核", systemUser.getUserId().toString(), "党委发文","to 办公室主任复核");
				flowTemplate.setVariable("流程名称", "党委发文", flowid);
			
			fenfalist = systemUserBo.userlistOrdery(fenfalist);
			flowTemplate.setVariable("封发", fenfalist, flowid);
			
		}
		
		dangweifawenNeirong =	selectOneById(id);
		dangweifawenNeirong.setFlowid(flowid.intValue());
		updateTDangweifawenneirong(dangweifawenNeirong);
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			TDangweifawenFujian dangweifawenFujian = new TDangweifawenFujian();
			dangweifawenFujian.setFujianAddress(file.toFilePath);
			dangweifawenFujian.setFujianName(file.getOldFileName());
			dangweifawenFujian.setFujianZhengname(file.getNewFileName());
			dangweifawenFujian.setTDangweifawenNeirong(dangweifawenNeirong);
			dangweifawenNeirongDAO.saveFuJian(dangweifawenFujian);
			

		}
		flowTemplate.setVariable("拟稿人修改内容", dangweifawenNeirong.getNigaorenid().toString(), flowid);
		return id;
		
	}

	//党委直接发文
	public String addTDangweizhijiefaweineirong(
			TDangweifawenNeirong dangweifawenNeirong,String taohongrenid,String zhusongid,String chaosongid,List<Files> list) {
		// TODO Auto-generated method stub
		
		String id = dangweifawenNeirongDAO.save(dangweifawenNeirong);
		List<String> fenfalist = new ArrayList<String>();
		String[] zhusongarray = zhusongid.split(",");
		for (int i = 0; i < zhusongarray.length; i++) {
			if (zhusongarray[i] !=null &&!"".equals(zhusongarray[i])) {
				fenfalist.add(zhusongarray[i]);
			}
		}
		if (chaosongid != null) {
			String[] chaosongarray = chaosongid.split(",");	
			for (int i = 0; i < chaosongarray.length; i++) {
				if (chaosongarray[i] !=null &&!"".equals(chaosongarray[i])) {
					fenfalist.add(chaosongarray[i]);
				}
			}
		}
		WorkFlowTemplate flowTemplate = new WorkFlowTemplate();
		Long flowid = 0l;
		if (dangweifawenNeirong.getState() == 1) {
			flowid =  flowTemplate.creatProcessInstance("套红", taohongrenid, "党委直接发文");
			flowTemplate.setVariable("流程名称", "党委发文", flowid);
		}else if (dangweifawenNeirong.getState() == 2) {
			flowid =  flowTemplate.creatProcessInstance("套红", taohongrenid, "团委直接发文");
			flowTemplate.setVariable("流程名称", "团委发文", flowid);
		}else if (dangweifawenNeirong.getState() == 3) {
			flowid =  flowTemplate.creatProcessInstance("套红", taohongrenid, "纪委直接发文");
			flowTemplate.setVariable("流程名称", "纪委发文", flowid);
		}else if (dangweifawenNeirong.getState() == 4) {
			flowid =  flowTemplate.creatProcessInstance("套红", taohongrenid, "工会直接发文");
			flowTemplate.setVariable("流程名称", "工会发文", flowid);
		}
			
		fenfalist = systemUserBo.userlistOrdery(fenfalist);
		flowTemplate.setVariable("封发", fenfalist, flowid);
		
		dangweifawenNeirong =	selectOneById(id);
		dangweifawenNeirong.setFlowid(flowid.intValue());
		updateTDangweifawenneirong(dangweifawenNeirong);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			TDangweifawenFujian dangweifawenFujian = new TDangweifawenFujian();
			dangweifawenFujian.setFujianAddress(file.toFilePath);
			dangweifawenFujian.setFujianName(file.getOldFileName());
			dangweifawenFujian.setFujianZhengname(file.getNewFileName());
			dangweifawenFujian.setTDangweifawenNeirong(dangweifawenNeirong);
			dangweifawenNeirongDAO.saveFuJian(dangweifawenFujian);
		}
		return id;
		
	}
	
	
	/**
	 * 添加党委发文内容
	 * @param 党委发文
	 * @return 党委发文ID
	 */
	public String addTDangweifaweineirong(TDangweifawenNeirong dangweifawenNeirong){
		String id  = "0";
		id =  dangweifawenNeirongDAO.save(dangweifawenNeirong);
		return id;
	}
	
	public TDangweifawenNeirong selectOneById(String id) {
		// TODO Auto-generated method stub
		return dangweifawenNeirongDAO.findById(id);
	}

	public Boolean updateTDangweifawenneirong(
			TDangweifawenNeirong dangweifawenNeirong) {
		// TODO Auto-generated method stub
		
		try {
			dangweifawenNeirongDAO.merge(dangweifawenNeirong);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public List<TDangweifawenNeirong> findByExample(
			TDangweifawenNeirong dangweifawenNeirong) {
		// TODO Auto-generated method stub
		List<TDangweifawenNeirong> list = dangweifawenNeirongDAO.findByExample(dangweifawenNeirong);
		return list;
	}

	
	
	/**
	 * 查询党委发文的总行数

	 * @return
	 */
	public Integer selectDangweifawenTotalLine(){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		return  qbcDao.getQueryNumber(detachedCriteria, "id");
	}
	
	/**
	 * 查询党委发文的总行数

	 * @return
	 */
	public Integer selectDangweifawenTotalLine(Integer state){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		detachedCriteria.add(Restrictions.eq("state", state));
		return  qbcDao.getQueryNumber(detachedCriteria, "id");
	}
	
	public List<TDangweifawenNeirong> selectDangweifawenPage(Integer first,Integer max){
		DetachedCriteria criteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		List<Order> orderlist = new ArrayList<Order>();
		List<TDangweifawenNeirong> list = qbcDao.getCriteria(criteria);
		Comparator comp = new Comparator(){
			public int compare(Object arg0, Object arg1) {
				// TODO Auto-generated method stub
				WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
				TDangweifawenNeirong p1=(TDangweifawenNeirong)arg0;
				TDangweifawenNeirong p2=(TDangweifawenNeirong)arg1;
				   if(workFlowTemplate.isSuspend(Long.valueOf(p1.getFlowid().toString())) && !workFlowTemplate.isSuspend(Long.valueOf(p2.getFlowid().toString()))){
					   return 1;
				   }else if(!workFlowTemplate.isSuspend(Long.valueOf(p1.getFlowid().toString())) && workFlowTemplate.isSuspend(Long.valueOf(p2.getFlowid().toString()))){
					   return 0;
				   }
				   if(p1.getWenhaonumber() == null && p2.getWenhaonumber() == null)
		             {
		            	 if(p1.getSortid() > p2.getSortid()){
		            		 return 0;
		            	 }else{
		            		 return 1;
		            	 }
		             }else  if(p1.getWenhaonumber() == null && p2.getWenhaonumber() != null)
		             {
		            	 return 0;
		             } 
		             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() == null)
		             {
		            	 return 1;
		             }
		             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() != null)
		             {
		            	 if(Integer.parseInt(p1.getWenhaotype().substring(p1.getWenhaotype().indexOf("2"), p1.getWenhaotype().indexOf("2")+4) ) > 
		            	 Integer.parseInt(p2.getWenhaotype().substring(p2.getWenhaotype().indexOf("2"), p2.getWenhaotype().indexOf("2")+4) ) )
		            	 {
		            		 return 0;
		            	 }else if(Integer.parseInt(p1.getWenhaotype().substring(p1.getWenhaotype().indexOf("2"), p1.getWenhaotype().indexOf("2") +4) ) < 
		            	 Integer.parseInt(p2.getWenhaotype().substring(p2.getWenhaotype().indexOf("2"),p2.getWenhaotype().indexOf("2") + 4) ) ){
		            		 return 1;
		            	 }else{
		            		 if(Integer.parseInt(p1.getWenhaonumber())  > Integer.parseInt(p2.getWenhaonumber()) ){
			            		 return 0;
			            	 }else{
			            		 return 1;
			            	 }
			             	}
		            	 }
			             
		              return 0;
		          
			}
			
		};
	        Collections.sort(list, comp);
	    if(first + max > list.size()){
	    	return list.subList(first, list.size());
	    } else {
	    	return list.subList(first, first+max);
	    }   
	}
	
	/**
	 * 条件查询党委发文的总行数

	 * @return
	 */
	public Integer selectDangweifawenTotalLineCriteria(DetachedCriteria criteria){
		return  qbcDao.getQueryNumber(criteria, "id");
	}
	
	
	/**
	 * 查询党委发文分页
	 * @return
	 */
	public List<TDangweifawenNeirong> selectDangweifawenPage(Integer first,Integer max,Integer state){
		DetachedCriteria criteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		criteria.add(Restrictions.eq("state", state));
		List<Order> orderlist = new ArrayList<Order>();
		List<TDangweifawenNeirong> list = qbcDao.getCriteria(criteria);
		Comparator comp = new Comparator(){
			public int compare(Object arg0, Object arg1) {
				// TODO Auto-generated method stub
				WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
				TDangweifawenNeirong p1=(TDangweifawenNeirong)arg0;
				TDangweifawenNeirong p2=(TDangweifawenNeirong)arg1;
				   if(workFlowTemplate.isSuspend(Long.valueOf(p1.getFlowid().toString())) && !workFlowTemplate.isSuspend(Long.valueOf(p2.getFlowid().toString()))){
					   return 1;
				   }else if(!workFlowTemplate.isSuspend(Long.valueOf(p1.getFlowid().toString())) && workFlowTemplate.isSuspend(Long.valueOf(p2.getFlowid().toString()))){
					   return 0;
				   }
				   if(p1.getWenhaonumber() == null && p2.getWenhaonumber() == null)
		             {
		            	 if(p1.getSortid() > p2.getSortid()){
		            		 return 0;
		            	 }else{
		            		 return 1;
		            	 }
		             }else  if(p1.getWenhaonumber() == null && p2.getWenhaonumber() != null)
		             {
		            	 return 0;
		             } 
		             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() == null)
		             {
		            	 return 1;
		             }
		             /*else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() != null)
		             {
		            	 if(Integer.parseInt(p1.getWenhaotype().substring(p1.getWenhaotype().indexOf("2"), p1.getWenhaotype().indexOf("2")+4) ) > 
		            	 Integer.parseInt(p2.getWenhaotype().substring(p2.getWenhaotype().indexOf("2"), p2.getWenhaotype().indexOf("2")+4) ) )
		            	 {
		            		 return 0;
		            	 }else if(Integer.parseInt(p1.getWenhaotype().substring(p1.getWenhaotype().indexOf("2"), p1.getWenhaotype().indexOf("2") +4) ) < 
		            	 Integer.parseInt(p2.getWenhaotype().substring(p2.getWenhaotype().indexOf("2"),p2.getWenhaotype().indexOf("2") + 4) ) ){
		            		 return 1;
		            	 }else{
		            		 if(Integer.parseInt(p1.getWenhaonumber())  > Integer.parseInt(p2.getWenhaonumber()) ){
			            		 return 0;
			            	 }else{
			            		 return 1;
			            	 }
			             	}
		            	 }*/
			             
		              return 0;
		          
			}
			
		};
	        Collections.sort(list, comp);
	    if(first + max > list.size()){
	    	return list.subList(first, list.size());
	    } else {
	    	return list.subList(first, first+max);
	    }   
	}
	
	/**
	 * 条件查询党委发文分页
	 * @return
	 */
	public List<TDangweifawenNeirong> selectDangweifawenPageCriteria(DetachedCriteria criteria, Integer first,Integer max){
		List<Order> orderlist = new ArrayList<Order>();
		List<TDangweifawenNeirong> list = qbcDao.getCriteria(criteria);
		 Comparator comp = new Comparator(){
	          public int compare(Object o1,Object o2) {
	        	  TDangweifawenNeirong p1=(TDangweifawenNeirong)o1;
	        	  TDangweifawenNeirong p2=(TDangweifawenNeirong)o2; 
	              if(p1.getWenhaonumber() == null && p2.getWenhaonumber() == null)
	             {
	            	 if(p1.getSortid() > p2.getSortid()){
	            		 return 0;
	            	 }else{
	            		 return 1;
	            	 }
	             }else  if(p1.getWenhaonumber() == null && p2.getWenhaonumber() != null)
	             {
	            	 return 0;
	             } 
	             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() == null)
	             {
	            	 return 1;
	             }
	             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() != null)
	             {
		             if(Integer.parseInt(p1.getWenhaonumber())  > Integer.parseInt(p2.getWenhaonumber()) ){
		            		 return 0;
		            	 }else{
		            		 return 1;
		            	 }
		             }
	              return 0;
	          	}  
	             
	        };
	        Collections.sort(list, comp);
	    if(first + max > list.size()){
	    	return list.subList(first, list.size());
	    } else {
	    	return list.subList(first, first+max);
	    }   
		
	}
	
	/**
	 * 条件查询党委发文分页
	 * @return
	 */
	public List<TDangweifawenNeirong> selectDangweifawenPageCriteria(DetachedCriteria criteria, Integer first,Integer max,String biaoti,String wenhao){
		List<Order> orderlist = new ArrayList<Order>();
		Disjunction disjunction = Restrictions.disjunction();
		disjunction.add(Restrictions.like("biaoti", "%"+biaoti+"%"));
		disjunction.add(Restrictions.like("wenhaotype", "%"+wenhao+"%"));
		List<TDangweifawenNeirong> list = qbcDao.getCriteria(criteria);
		 Comparator comp = new Comparator(){
	          public int compare(Object o1,Object o2) {
	        	  TDangweifawenNeirong p1=(TDangweifawenNeirong)o1;
	        	  TDangweifawenNeirong p2=(TDangweifawenNeirong)o2; 
	              if(p1.getWenhaonumber() == null && p2.getWenhaonumber() == null)
	             {
	            	 if(p1.getSortid() > p2.getSortid()){
	            		 return 0;
	            	 }else{
	            		 return 1;
	            	 }
	             }else  if(p1.getWenhaonumber() == null && p2.getWenhaonumber() != null)
	             {
	            	 return 0;
	             } 
	             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() == null)
	             {
	            	 return 1;
	             }
	             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() != null)
	             {
		             if(Integer.parseInt(p1.getWenhaonumber())  > Integer.parseInt(p2.getWenhaonumber()) ){
		            		 return 0;
		            	 }else{
		            		 return 1;
		            	 }
		             }
	              return 0;
	          	}  
	             
	        };
	        Collections.sort(list, comp);
	    if(first + max > list.size()){
	    	return list.subList(first, list.size());
	    } else {
	    	return list.subList(first, first+max);
	    }   
		
	}
	
	/**
	 * 根据文号类型查询最大值

	 * @param 文号类型名称
	 * @return
	 */
	public Integer selectDangweifawenMaxNumber(String numbertype){
	/*	DetachedCriteria criteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
	//	criteria.add(Restrictions.like("wenhaotype", numbertype+"〔"+(new Date().getYear() +1900)+"〕" +"%"));
		criteria.add(Restrictions.eq("wenyear", new Date().getYear() +1900));
		criteria.add(Restrictions.eq("wentype", numbertype));
		
		if (qbcDao.getCriteria(criteria).size() ==0) {
			return 1;
		} else {
			List<TDangweifawenNeirong> list = qbcDao.getCriteria(criteria);
			Collections.sort(list, new Comparator<TDangweifawenNeirong>(){

				public int compare(TDangweifawenNeirong o1,
						TDangweifawenNeirong o2) {
					// TODO Auto-generated method stub
					if(o1.getWenhaonumber() == null){
						return 1;
					}else if(o2.getWenhaonumber() == null){
						return 0;
					}else{
						if(Integer.parseInt(o1.getWenhaonumber()) > Integer.parseInt(o2.getWenhaonumber()) ){
							return 0;
						} else if(Integer.parseInt(o1.getWenhaonumber()) < Integer.parseInt(o2.getWenhaonumber()) ){
							return 1;
						} else {
							return 1;
						}
					}
					
				}
				
			});
			if(list.get(0) == null){
				return 1;
			}else{
				if(list.get(0).getWenhaonumber() == null){
					return 1;
				}else{
					return Integer.parseInt(list.get(0).getWenhaonumber()) + 1;
					
				}
				
			}
			 
		}
		
		*/
		
		return selectfawenMaxNumber(numbertype, "dbo.t_dangweifawen_neirong");
	}
	
	public Integer selectfawenMaxNumber(String numbertype, String tablename){
		String sql = new String("SELECT MAX(wenhao) as maxwenhao FROM " + tablename + " WHERE wenyear = ? AND wentype = ?");
		List<Integer> list =  springDao.querySQL(sql, new Object[]{new Date().getYear() +1900,numbertype}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				String maxvalue = arg0.getString("maxwenhao");
				Integer maxwenhao=1;
				if (null!=maxvalue){
					maxwenhao = Integer.valueOf(maxvalue) + 1;
				}
				return maxwenhao;
			}
			
		});
		return list.get(0);
	}

	public List<TDangweifawenNeirong> selectDangWeiFaWenSuperDaiBanList(
			Integer subDataBase, Integer dataBaseId, Integer userId,
			String proname) {
		// TODO Auto-generated method stub
		return systemUserBo.selectDangWeiFaWenSuperDaiBanList(subDataBase,1, userId, proname);
	}
	
	public List<TaoHongMoBan> selectTaoHongMoBan(TaoHongMoBan taoHongMoBan) {
		// TODO Auto-generated method stub
		String sql = new String("SELECT * FROM  dbo.t_system_taohongmuban WHERE flowtype = ? AND state = ? ");
		List<TaoHongMoBan> list =  springDao.querySQL(sql, new Object[]{taoHongMoBan.getFlowType(),taoHongMoBan.getState()}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TaoHongMoBan hongMoBan = new TaoHongMoBan();
				hongMoBan.setFileName(arg0.getString("fileName"));
				hongMoBan.setViewName(arg0.getString("viewName"));
				return hongMoBan;
			}
			
		});
		return list;
	}

	public List<YinZhang> selectYinZhang(YinZhang yinZhang) {
		// TODO Auto-generated method stub
		String sql = new String("SELECT * FROM  dbo.t_system_yinzhanglist WHERE flowtype = ? AND state = ? ");
		List<YinZhang> list =  springDao.querySQL(sql, new Object[]{yinZhang.getFlowType(),yinZhang.getState()}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				YinZhang hongMoBan = new YinZhang();
				hongMoBan.setFileName(arg0.getString("fileName"));
				hongMoBan.setViewName(arg0.getString("viewName"));
				return hongMoBan;
			}
			
		});
		return list;
	}

	public List<TDangweifawenNeirong> selectDangweifawenPage(Integer first,
			Integer max, Integer state, String biaoti, String wenhao) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		criteria.add(Restrictions.eq("state", state));
		Disjunction disjunction = Restrictions.disjunction();
		if(biaoti != null && !"".equals(biaoti)){
			disjunction.add(Restrictions.like("biaoti", "%"+biaoti+"%"));
		}
		if(wenhao != null && !"".equals(wenhao)){
			disjunction.add(Restrictions.like("wenhaotype", "%"+wenhao+"%"));
		}
		criteria.add(disjunction);
		List<Order> orderlist = new ArrayList<Order>();
		List<TDangweifawenNeirong> list = qbcDao.getCriteria(criteria);
		Comparator comp = new Comparator(){
			public int compare(Object arg0, Object arg1) {
				// TODO Auto-generated method stub
				WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
				TDangweifawenNeirong p1=(TDangweifawenNeirong)arg0;
				TDangweifawenNeirong p2=(TDangweifawenNeirong)arg1;
				   if(workFlowTemplate.isSuspend(Long.valueOf(p1.getFlowid().toString())) && !workFlowTemplate.isSuspend(Long.valueOf(p2.getFlowid().toString()))){
					   return 1;
				   }else if(!workFlowTemplate.isSuspend(Long.valueOf(p1.getFlowid().toString())) && workFlowTemplate.isSuspend(Long.valueOf(p2.getFlowid().toString()))){
					   return 0;
				   }
				   if(p1.getWenhaonumber() == null && p2.getWenhaonumber() == null)
		             {
		            	 if(p1.getSortid() > p2.getSortid()){
		            		 return 0;
		            	 }else{
		            		 return 1;
		            	 }
		             }else  if(p1.getWenhaonumber() == null && p2.getWenhaonumber() != null)
		             {
		            	 return 0;
		             } 
		             else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() == null)
		             {
		            	 return 1;
		             }
		             /*else  if(p1.getWenhaonumber() != null && p2.getWenhaonumber() != null)
		             {
		            	 if(Integer.parseInt(p1.getWenhaotype().substring(p1.getWenhaotype().indexOf("2"), p1.getWenhaotype().indexOf("2")+4) ) > 
		            	 Integer.parseInt(p2.getWenhaotype().substring(p2.getWenhaotype().indexOf("2"), p2.getWenhaotype().indexOf("2")+4) ) )
		            	 {
		            		 return 0;
		            	 }else if(Integer.parseInt(p1.getWenhaotype().substring(p1.getWenhaotype().indexOf("2"), p1.getWenhaotype().indexOf("2") +4) ) < 
		            	 Integer.parseInt(p2.getWenhaotype().substring(p2.getWenhaotype().indexOf("2"),p2.getWenhaotype().indexOf("2") + 4) ) ){
		            		 return 1;
		            	 }else{
		            		 if(Integer.parseInt(p1.getWenhaonumber())  > Integer.parseInt(p2.getWenhaonumber()) ){
			            		 return 0;
			            	 }else{
			            		 return 1;
			            	 }
			             	}
		            	 }*/
			             
		              return 0;
		          
			}
			
		};
	        Collections.sort(list, comp);
	    if(first + max > list.size()){
	    	return list.subList(first, list.size());
	    } else {
	    	return list.subList(first, first+max);
	    }   
	}
	
	/**
	 * 查询党委发文的总行数
	 * @return
	 */
	public Integer selectDangweifawenTotalLine(Integer state,String biaoti,String wenhao){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TDangweifawenNeirong.class);
		detachedCriteria.add(Restrictions.eq("state", state));
		Disjunction disjunction = Restrictions.disjunction();
		if(biaoti != null && !"".equals(biaoti)){
			disjunction.add(Restrictions.like("biaoti", "%"+biaoti+"%"));
		}
		if(wenhao != null && !"".equals(wenhao)){
			disjunction.add(Restrictions.like("wenhaotype", "%"+wenhao+"%"));
		}
		
		detachedCriteria.add(disjunction);
		return  qbcDao.getQueryNumber(detachedCriteria, "id");
	}
	
	/**
	 * 根据ID查询党委发文
	 * @param id
	 * @return
	 */
	public Boolean deleteOneById(String id){
		
		TDangweifawenNeirong dangweifawenNeirong = selectOneById(id);
		dangweifawenNeirongDAO.delete(dangweifawenNeirong);
		return true;
	}
	
	/**
	 * 根据ID查询党委发文
	 * @param id
	 * @return
	 */
	public Boolean deleteOneById(String id,Integer taskId){
		StringBuffer sql = new StringBuffer("SELECT * FROM dbo.JBPM_TASKINSTANCE WHERE ID_ = ? ");
		List<Integer> idList =  springDao.querySQL(sql.toString(), new Object[]{taskId},new RowMapper() {
			
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getInt("TOKEN_");
			}
		});
		if (idList.size() >  0) {
			Integer token =  idList.get(0);
			StringBuffer sql1 = new StringBuffer("delete from JBPM_LOG where 1=1 or TOKEN_ = ? ");
			springDao.commonUpdate(sql1.toString(), new Object[]{token});
			StringBuffer sql2 = new StringBuffer("delete from JBPM_TASKINSTANCE where ID_ = ? ");
			springDao.commonUpdate(sql2.toString(), new Object[]{taskId});
		}
		TDangweifawenNeirong dangweifawenNeirong = selectOneById(id);
		dangweifawenNeirongDAO.delete(dangweifawenNeirong);
		return true;
	}
	
	public Boolean deleteTask(Integer taskId){
		try {
			StringBuffer sql = new StringBuffer("delete from  dbo.JBPM_LOG WHERE TASKINSTANCE_ = ?  ");
			springDao.commonUpdate(sql.toString(), new Object[]{taskId});
			 sql = new StringBuffer("delete from JBPM_TASKINSTANCE where ID_ = ? ");
			springDao.commonUpdate(sql.toString(), new Object[]{taskId});
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	public String addqfTDangweifaweineirong(
			TDangweifawenNeirong dangweifawenNeirong,String huiqian, String zhusongid,
			String chaosongid, List<Files> list) {
		// TODO Auto-generated method stub
		String id = dangweifawenNeirongDAO.save(dangweifawenNeirong);
		List<String> fenfalist = new ArrayList<String>();
		List<String> huiqianlist = new ArrayList<String>();
		String[] zhusongarray = zhusongid.split(",");
		for (int i = 0; i < zhusongarray.length; i++) {
			if (zhusongarray[i] !=null &&!"".equals(zhusongarray[i])) {
				fenfalist.add(zhusongarray[i]);
			}
		}
		if (chaosongid != null) {
			String[] chaosongarray = chaosongid.split("、");	
			for (int i = 0; i < chaosongarray.length; i++) {
				if (chaosongarray[i] !=null &&!"".equals(chaosongarray[i])) {
					fenfalist.add(chaosongarray[i]);
				}
			}
		}
		String huiqianid="1";
		if (huiqian != null) {     
			String[] huiqianarray = huiqian.split("、");	
			if (huiqianarray[0] !=null &&!"".equals(huiqianarray[0])) {
				huiqianid=huiqianarray[0];
			}
			for (int i = 1; i < huiqianarray.length; i++) {
				if (huiqianarray[i] !=null &&!"".equals(huiqianarray[i])) {
					huiqianlist.add(huiqianarray[i]);
				}
			}
		}
		
		WorkFlowTemplate flowTemplate = new WorkFlowTemplate();
		Long flowid = 0l;
		String regEx = "[\u4e00-\u9fa5]";
		Pattern pat = Pattern.compile(regEx);
		//boolean zsid=isContainChinese(zhusongid);
		boolean csid=isContainChinese(chaosongid);
		if(csid==false){
			if (fenfalist.size()!=0) {
				fenfalist = systemUserBo.userlistOrdery(fenfalist);
				flowid =  flowTemplate.creatProcessInstance("封发", fenfalist, "党委发文","to 封发");
				flowTemplate.setVariable("流程名称", "党委发文", flowid);		
				flowTemplate.setVariable("封发", fenfalist, flowid);
			}else{
				String[] zhusong = dangweifawenNeirong.getZhusong().split(",");	
				List<String> zhusongList = new ArrayList<String>();
				Collections.addAll(zhusongList, zhusong);
				flowid =  flowTemplate.creatProcessInstance("封发",zhusongList, "行政发文","to 封发");
				flowTemplate.setVariable("流程名称", "党委发文", flowid);	
				flowTemplate.setVariable("封发", zhusongList, flowid);
			}
		}else{
			String[] zhusong = dangweifawenNeirong.getZhusong().split(",");	
			List<String> zhusongList = new ArrayList<String>();
			Collections.addAll(zhusongList, zhusong);
			flowid =  flowTemplate.creatProcessInstance("封发",zhusongList, "党委发文","to 封发");
			flowTemplate.setVariable("流程名称", "党委发文", flowid);	
			flowTemplate.setVariable("封发", zhusongList, flowid);
		}	
		
		dangweifawenNeirong =	selectOneById(id);
		dangweifawenNeirong.setFlowid(flowid.intValue());
		updateTDangweifawenneirong(dangweifawenNeirong);
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next(); 
			String chaobao=dangweifawenNeirong.getChaobao();
			String miji=dangweifawenNeirong.getMiji();
			String fujian=dangweifawenNeirong.getFujian();
			boolean a=dangweifawenNeirong.getChaobao().contains(",");
			boolean b=dangweifawenNeirong.getMiji().contains(",");
			boolean c=dangweifawenNeirong.getFujian().contains(",");
			if(chaobao!=null){
				if(a==true){
					String[] chaobaoarray=dangweifawenNeirong.getChaobao().split(",");
					for(int i=1;i<chaobaoarray.length;i++){
						if(chaobaoarray[i]!=null && !"".equals(chaobaoarray[i])){
							if(chaobaoarray[i].equals(file.oldFileName)){
								TDangweifawenZhengwen dangweifawenZhengwen=new TDangweifawenZhengwen();
								dangweifawenZhengwen.setzhengwenAddress(file.toFilePath);
								dangweifawenZhengwen.setzhengwenName(file.getOldFileName());
								dangweifawenZhengwen.setzhengwenZhengname(file.getNewFileName());
								dangweifawenZhengwen.setTDangweifawenNeirong(dangweifawenNeirong);
								dangweifawenNeirongDAO.saveZhengWen(dangweifawenZhengwen);
							}
						}
					}
				}else{
					if(dangweifawenNeirong.getChaobao().equals(file.oldFileName)){
						TDangweifawenZhengwen dangweifawenZhengwen=new TDangweifawenZhengwen();
						dangweifawenZhengwen.setzhengwenAddress(file.toFilePath);
						dangweifawenZhengwen.setzhengwenName(file.getOldFileName());
						dangweifawenZhengwen.setzhengwenZhengname(file.getNewFileName());
						dangweifawenZhengwen.setTDangweifawenNeirong(dangweifawenNeirong);
						dangweifawenNeirongDAO.saveZhengWen(dangweifawenZhengwen);
					}
				}
			}
			if(miji!=null){
				if(b==true){
					String[] mijiarray=dangweifawenNeirong.getMiji().split(",");
					for(int i=1;i<mijiarray.length;i++){
						if(mijiarray[i]!=null && !"".equals(mijiarray[i])){
							if(mijiarray[i].equals(file.oldFileName)){
								TDangweifawenBeizhu dangweifawenBeizhu=new TDangweifawenBeizhu();
								dangweifawenBeizhu.setbeizhuAddress(file.toFilePath);
								dangweifawenBeizhu.setbeizhuName(file.getOldFileName());
								dangweifawenBeizhu.setbeizhuZhengname(file.getNewFileName());
								dangweifawenBeizhu.setTDangweifawenNeirong(dangweifawenNeirong);
								dangweifawenNeirongDAO.saveBeizhu(dangweifawenBeizhu);
							}
						}
					}
				}else{
					if(dangweifawenNeirong.getMiji().equals(file.oldFileName)){
						TDangweifawenBeizhu dangweifawenBeizhu=new TDangweifawenBeizhu();
						dangweifawenBeizhu.setbeizhuAddress(file.toFilePath);
						dangweifawenBeizhu.setbeizhuName(file.getOldFileName());
						dangweifawenBeizhu.setbeizhuZhengname(file.getNewFileName());
						dangweifawenBeizhu.setTDangweifawenNeirong(dangweifawenNeirong);
						dangweifawenNeirongDAO.saveBeizhu(dangweifawenBeizhu);
					}
				}
			}if(fujian!=null){
				if(c==true){
					String[] fujianarray=dangweifawenNeirong.getFujian().split(",");
					for(int i=1;i<fujianarray.length;i++){
						if(fujianarray[i]!=null && !"".equals(fujianarray[i])){
							if(fujianarray[i].equals(file.oldFileName)){
								TDangweifawenFujian dangweifawenFujian=new TDangweifawenFujian();
								dangweifawenFujian.setFujianAddress(file.toFilePath);
								dangweifawenFujian.setFujianName(file.getOldFileName());
								dangweifawenFujian.setFujianZhengname(file.getNewFileName());
								dangweifawenFujian.setTDangweifawenNeirong(dangweifawenNeirong);
								dangweifawenNeirongDAO.saveFuJian(dangweifawenFujian);
							}
						}
					}
				}else{
					if(dangweifawenNeirong.getFujian().equals(file.oldFileName)){
						TDangweifawenFujian dangweifawenFujian=new TDangweifawenFujian();
						dangweifawenFujian.setFujianAddress(file.toFilePath);
						dangweifawenFujian.setFujianName(file.getOldFileName());
						dangweifawenFujian.setFujianZhengname(file.getNewFileName());
						dangweifawenFujian.setTDangweifawenNeirong(dangweifawenNeirong);
						dangweifawenNeirongDAO.saveFuJian(dangweifawenFujian);
					}
				}
			}
		}
		
		return id;
	}

	private boolean isContainChinese(String str) {
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
	}
}
